//
//  TabBarController.m
//  tuanGG
//
//  Created by 微他 on 15/9/16.
//  Copyright (c) 2015年 weita. All rights reserved.
//

#import "TabBarController.h"
#import "ViewController.h"
#import "Header.h"
#import "TabBarButton.h"
#import "GoodsManagementController.h"
#import "DataStatisticsController.h"
#import "OtherManagementController.h"
@interface TabBarController (){
    UIImageView *_tabBarView;//自定义tabBar
    TabBarButton *_oldButton;//记录前一次选择de1按钮
    CGRect _rect;
}
@end

@implementation TabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //添加自己的ViewController
    GoodsManagementController *vc1 = [[GoodsManagementController alloc]init];
//    vc1.view.backgroundColor = [UIColor redColor];
    UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:vc1];
//    nav1.navigationBar.barTintColor = [UIColor blueColor];
    
    OtherManagementController *vc2 = [[OtherManagementController alloc]init];
    UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:vc2];
    
    DataStatisticsController *vc3 = [[DataStatisticsController alloc]init];
    UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:vc3];
    
//    YGMoreController *vc4 = [[YGMoreController alloc]init];
//    UINavigationController *nav4 = [[UINavigationController alloc]initWithRootViewController:vc4];
//    ViewController *vc5 = [[ViewController alloc]init];

//    UINavigationController *nav5 = [[UINavigationController alloc]initWithRootViewController:vc5];
    
    self.viewControllers = @[nav1,nav2,nav3];
    [self creatTabBarView];;
}

#pragma mark - 创建TabBarView

- (void)creatTabBarView {
    self.tabBar.hidden = YES;
    _tabBarView = [[UIImageView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.bounds) - tabBarHeight, CGRectGetWidth(self.view.bounds), tabBarHeight)];
    _rect = _tabBarView.frame;
    _tabBarView.userInteractionEnabled = YES;
    _tabBarView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_tabBarView];
    
    [self creatButtonItem:@[@"商品管理",@"订单管理",@"数据统计"]
               imageArray:@[@"商品管理_off",@"TabBarOrderNormal@2x",@"TabBarDataNormal@2x"] imageSelectArray:@[@"商品管理",@"TabBarOrder@2x",@"TabBarData@2x"]];
}

- (void)creatButtonItem:(NSArray *)array imageArray:(NSArray *)imageArray imageSelectArray:(NSArray *)imageSelectArray{
    
    CGFloat buttonW = CGRectGetWidth(self.view.bounds)/array.count;
    CGFloat buttonH = CGRectGetHeight(_tabBarView.bounds);
    
    for (int i = 0 ; i < array.count; i ++) {
        TabBarButton *customButton = [TabBarButton buttonWithType:UIButtonTypeCustom];
        customButton.tag = i;
        [customButton setTitle:array[i] forState:UIControlStateNormal];
        [customButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [customButton setTitleColor:[UIColor colorWithWhite:0.702 alpha:1.000] forState:UIControlStateNormal];
        [customButton setImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];
        [customButton setImage:[UIImage imageNamed:imageSelectArray[i]] forState:UIControlStateSelected];
        [customButton addTarget:self action:@selector(customButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        customButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        customButton.titleLabel.font = [UIFont systemFontOfSize:13];
        customButton.frame = CGRectMake(buttonW *i, 0, buttonW, buttonH);
        [_tabBarView addSubview:customButton];
        
        if(i == 0)//设置第一个选择项。（默认选择项）
        {
            _oldButton = customButton;
            _oldButton.selected = YES;
        }
    }
}

#pragma mark - 点击事件

- (void)customButtonPressed:(TabBarButton *)sender {
    if(self.selectedIndex != sender.tag){ //wsq®
        self.selectedIndex = sender.tag; //切换不同控制器的界面
        _oldButton.selected = ! _oldButton.selected;
        _oldButton = sender;
        _oldButton.selected = YES;
    }
}

#pragma mark - 弹出消失动画

- (void)showTabBar {
    [UIView animateWithDuration:0.2 animations:^{
        _tabBarView.frame = _rect;
    }];
}

- (void)hidTabBar {
    [UIView animateWithDuration:0.2 animations:^{
        _tabBarView.frame = CGRectMake(0, CGRectGetHeight(self.view.bounds), CGRectGetWidth(self.view.bounds), tabBarHeight);
    }];
    
}


@end
